<template>
	<view class="content">
		<view class="add-box">
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="150" label="车牌号码" prop="carNumber">
					<u-input v-model="form.carNumber" placeholder="请输入车牌" />
				</u-form-item>
				<u-form-item label-width="150" label="司机姓名" prop="driverName">
					<u-input v-model="form.driverName" placeholder="请输入司机姓名" />
				</u-form-item>
				<u-form-item label-width="150" label="联系电话" prop="driverPhone">
					<u-input v-model="form.driverPhone" placeholder="请输入司机联系电话" />
				</u-form-item>
<!-- 				<u-form-item label-width="150" label="作业点">
					<u-input v-model="siteId" @click="selectSiteShow = true" placeholder="请选择作业点" type="select" />
				</u-form-item> -->
				<u-form-item label-width="150" label="绑定设备" prop="iotId">
					<u-input v-model="form.iotId"  placeholder="请输入设备编码" :clearable="false"/>
					<u-icon name="camera-fill" color="#00aaff" size="40rpx" @tap="photo()"></u-icon>
				</u-form-item>
				<u-form-item label-width="160" label="图片上传" >
					<u-upload width="160" height="160" :form-data="queryOss" :action="action" :auto-upload="false" ref="uUpload"
					 max-count="1" :show-progress="false" :multiple="false" @on-choose-complete="changeImg()"></u-upload>
				</u-form-item>
<!-- 				<u-form-item label-width="150" label="车辆类型">
					<u-input v-model="type" @click="selectTypeShow = true" placeholder="请选择项车辆类型" type="select" />
				</u-form-item> -->
			</u-form>
		</view>
		<view class="button-box">
			<u-button type="primary" @tap="addCar">添加</u-button>
		</view>
<!-- 		<view class="nav" v-show="isclick">
			<navigator url="/pages/project/pointMangement/addSite/index" hover-class="navigator-hover">
				<view>没有作业点？前往添加 &nbsp; <u-icon name="arrow-right"></u-icon></view>
			</navigator>
		</view> -->
		<u-select mode="single-column" :list="selectSwitch" v-model="selectTypeShow" @confirm="selectTypeConfirm"></u-select>
		<u-select mode="single-column" :list="selectList" v-model="selectSiteShow" @confirm="selectSiteConfirm"></u-select>
	</view>
</template>

<script>
	import {
		getSite
	} from "@/api/site.js"
	import {
		createCar
	} from "@/api/car.js"
	import {
		ossimg
	} from '@/api/oss.js'
	export default {
		data() {
			return {
				form: {
					action: 'team',
					carNumber: "",
					driverName: "",
					driverPhone: "",
					type: 1,
					projectId: null,
					// siteId: null,
					teamId: null,
					iotId: null
				},
				isclick: false,
				siteId: '',
				type: '',
				action: '',
				queryOss: {},
				dir:'',
				selectTypeShow: false,
				selectSiteShow: false,
				selectList: [],
				selectSwitch: [{
						label: "运输车",
						value: 1
					},
					{
						label: "挖掘机",
						value: 2
					}
				],
				rules: {
					carNumber: [{
						required: true,
						message: '请输入车牌号',
						trigger: ['blur'],
					}],
					driverName: [{
						required: true,
						message: '请输入司机姓名',
						trigger: ['blur'],
					}],
					driverPhone: [{
						required: true,
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// this.$u.test.mobile()就是返回true或者false的
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: 'blur',
					}],
					iotId: [{
						required: true,
						message: '请绑定设备',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
				}
			};
		},
		created() {
			// this.getSiteData()
			this.getOss()
		},
		onShow() {
			var pages = getCurrentPages();
			this.prevPage = pages[pages.length - 1];
			if (this.prevPage.brand === true) {
				this.getSiteData()
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			// 选择商品类型回调
			selectTypeConfirm(e) {
				e.map((val, index) => {
					this.form.type = val.value
					this.type = val.label
				});
			},
			selectSiteConfirm(e) {
				e.map((val, index) => {
					this.form.siteId = val.value
					this.siteId = val.label
				});
			},
			changeImg(event) {
				let files = this.$refs.uUpload.lists;
				this.queryOss.Filename = files[0].file.name
				const imgFormat = files[0].url.split('/').pop().split('.')[1];
				const imgName = files[0].url.split('/').pop().split('.')[0];
				const imgMd5Name = this.$md5(imgName);
				this.queryOss.key = `${this.dir}${imgMd5Name}.${imgFormat}`
				this.form.logo = this.action + '/' + this.queryOss.key
			},
			getOss(){
				ossimg({
					dir: 'car'
				}).then(res => {
					console.log(res);
					this.queryOss = res.result
					const {
						host,
						OSSAccessKeyId,
						policy,
						signature,
						dir
					} = res.result
					this.queryOss = {
						'OSSAccessKeyId': OSSAccessKeyId,
						'policy': policy,
						'signature': signature,
						'success_action_status': '200',
					}
					this.action = res.result.host
					this.dir = dir
					console.log(this.action);
				})
			},
			photo(){
				// console.log(bool)
				uni.scanCode({
					scanType:['qrCode'],
				    success: (res) =>{
						this.form.iotId = res.result
						console.log(res);
				    },
					fail: err =>{
						uni.showToast({
							title: "扫码失败",
							icon: "none",
							duration: 2000
						})
					}
				});
			},
			getSiteData() {
				this.selectList = []
				getSite({
					projectId: uni.getStorageSync('id')
				}).then(res => {
					console.log(res)
					if(res.code !== 1){
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
						return
					}
					if(res.result.length === 0){
						this.isclick = true
						// console.log("没有东西")
						// uni.showModal({
						// 	title: "提示",
						// 	content: "未检测到工程点位，请添加",
						// 	showCancel: false,
						// 	success: res =>{
						// 		if(res.confirm){
						// 			uni.navigateTo({
						// 				url:'/pages/project/pointMangement/addSite/index'
						// 			})
						// 		}
						// 	}
						// })
						return
					}
					res.result.forEach((item, index) => {
						let lemp = {}
						lemp.label = item.name
						lemp.value = item.id
						this.selectList.push(lemp)
					})
					console.log(this.selectList)
				})
			},
			addCar() {
				this.form.projectId = uni.getStorageSync('id')
				this.form.teamId = uni.getStorageSync('teamId')
				if (this.form.type === null || this.form.siteId === null) {
					uni.showToast({
						title: "不能为空",
						icon: "none"
					})
					return
				}
				this.$refs.uForm.validate(val =>{
					if(val){
						this.$refs.uUpload.upload();
						createCar(this.form).then(res => {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
							// console.log(res)
							if (res.code !== 1) {
								return
							}
							setTimeout(() => {
								var item = true; //item为要传的值
								var pages = getCurrentPages();
								var prevPage = pages[pages.length - 2];
								prevPage.brand = item;
								uni.navigateBack()
							}, 500)
						
						})
					}
				})
				

			}

		}
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx;

		.add-box {
			background-color: #ffffff;
			border-radius: 10rpx;
			padding: 20rpx;
		}

		.button-box {
			margin-top: 80rpx;
		}
	}
	.nav{
		text-align: center;
		margin-top: 100rpx;
		color: blue;
	}
</style>
